<template>
  <!--上传图片-->
  <!--                el-upload写在form中会自动提交，导致页面自动刷新-->
  <!--                在form标签中加上@submit.native.prevent-->
  <div class="upload-image-index">
    <order-module-title title="图片查看-上传"/>
    <div class="upload-image-body">
      <div v-if="images.length > 0" style="float: left">
        <img v-for="item in images" :key="item"  alt="" :src="item" class="avatar">
        <div style="clear: both"></div>
      </div>
      <el-upload
          class="avatar-uploader"
          action="/api/common/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
        <i class="el-icon-plus avatar-uploader-icon" style="float:left;"></i>
        <div style="clear: both"></div>
      </el-upload>
    </div>
  </div>
</template>

<script>
import OrderModuleTitle from "@/components/order/component/OrderModuleTitle";

export default {
  name: "UploadImage",
  components: {
    OrderModuleTitle,

  },
  data() {
    return {
      images: [],
      headers: {
        // token: localStorage.getItem("token"),
      },
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.images.push(`/api/common/download?name=${res.data}`)
    },
    beforeAvatarUpload(file) {
      // 图片上传数量限制
      if(this.images.length > 6){
        this.$message.error("最多上传6张图片")
        return;
      }
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;
      // 上传格式
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      // 上传图片大小
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  }
}
</script>

<style scoped lang="scss">


.upload-image-body {
  margin: 10px 0;


}

::v-deep .avatar-uploader .el-upload {
  border: 1px black solid;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

::v-deep .avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
  margin-right: 20px;
  border-radius: 6px;
  float: left;
}
</style>